/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved.
 * MemCache_Hybrid is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
*/
#ifndef MEM_FABRIC_HYBRID_MMC_DEFINE_H
#define MEM_FABRIC_HYBRID_MMC_DEFINE_H

#define MMC_DATA_TTL_MS 2000
#define MMC_THRESHOLD_PRINT_SECONDS 30
#define MMC_THREAD_POOL_MAX_THREADS 1024

namespace ock {
namespace mmc {
// macro for gcc optimization for prediction of if/else
#ifndef LIKELY
#define LIKELY(x) (__builtin_expect(!!(x), 1) != 0)
#endif

#ifndef UNLIKELY
#define UNLIKELY(x) (__builtin_expect(!!(x), 0) != 0)
#endif

/**
 * @brief Set last error message and log it
 *
 * @param msg              [in] message to be set and log
 */
#define MMC_LOG_AND_SET_LAST_ERROR(msg)  \
    do {                                 \
        std::stringstream tmpStr;        \
        tmpStr << msg;                   \
        MmcLastError::Set(tmpStr.str()); \
        MMC_LOG_ERROR(tmpStr.str());     \
    } while (0)

/**
 * @brief Set last error message
 *
 * @param msg              [in] message to be set
 */
#define MMC_SET_LAST_ERROR(msg)          \
    do {                                 \
        std::stringstream tmpStr;        \
        tmpStr << msg;                   \
        MmcLastError::Set(tmpStr.str()); \
    } while (0)

/**
 * @brief Set last error message and print to stdout
 *
 * @param msg              [in] message to be set and printed
 */
#define MMC_COUT_AND_SET_LAST_ERROR(msg) \
    do {                                 \
        std::stringstream tmpStr;        \
        tmpStr << msg;                   \
        MmcLastError::Set(tmpStr.str()); \
        std::cout << msg << std::endl;   \
    } while (0)

/**
* @brief Validate expression, if expression is false then
 * 1 set last error message
 * 2 log an error message
 * 3 return the specified value
 *
 * @param expression       [in] expression to be validated
 * @param msg              [in] message to set and log
 * @param returnValue      [in] return value
 */
#define MMC_VALIDATE_RETURN(expression, msg, returnValue) \
    do {                                                  \
        if (UNLIKELY(!(expression))) {                    \
            MMC_SET_LAST_ERROR(msg);                      \
            MMC_LOG_ERROR(msg);                           \
            return returnValue;                           \
        }                                                 \
    } while (0)

/**
 * @brief Validate expression, if expression is false then
 * 1 set last error message
 * 2 log an error message
 * 3 return
 *
 * @param expression       [in] expression to be validated
 * @param msg              [in] message to set and log
 */
#define MMC_VALIDATE_RETURN_VOID(expression, msg) \
    do {                                          \
        if (UNLIKELY(!(expression))) {            \
            MMC_SET_LAST_ERROR(msg);              \
            MMC_LOG_ERROR(msg);                   \
            return;                               \
        }                                         \
    } while (0)

#define MMC_API __attribute__((visibility("default")))

/**
 * @brief Delete a ptr safely, i.e. delete and set to nullptr
 *
 * @param p                [in] ptr to be deleted
 */
#define SAFE_DELETE(p) \
    do {               \
        delete (p);    \
        p = nullptr;   \
    } while (0)

}  // namespace mmc
}  // namespace ock

#endif  // MEM_FABRIC_HYBRID_MMC_DEFINE_H